
public class MyLinkedList implements IList{

    //我们对于双向无头非非循环链表的节点呢，我们也是使用内部类来构成的
    static class ListNode {
        public int val;
        public ListNode prev;//默认为null
        public ListNode next;//默认为null

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);

        if (this.head == null) {
            this.head = this.last = newNode;
        }else {
            newNode.next = this.head;
            this.head.prev = newNode;
            this.head = newNode;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);

        if (this.head == null) {
            this.head = this.last = newNode;
        }else {
            this.last.next = newNode;
            newNode.prev = this.last;
            this.last = this.last.next;
        }
    }

    private void indexException2(int index) throws IndexException{
        if (index < 0 || index > size()) {
            throw new IndexException("index错误");
        }
    }

    private ListNode findKeyNode(int index) {
        ListNode cur = this.head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    @Override
    public void addIndex(int index, int data) {
        try{
            indexException2(index);

            if (index == 0) {
                addFirst(data);
                return;
            }
            if (index == size()) {
                addLast(data);
                return;
            }

            //处理中间
            ListNode cur = findKeyNode(index);
            ListNode newNode = new ListNode(data);
            //处理next
            newNode.next = cur;
            cur.prev.next = newNode;
            //处理prev
            newNode.prev = cur.prev;
            cur.prev = newNode;
        }catch (IndexException e) {
            System.out.println("index输入错误");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if (cur.val == key) {
                //开始删除
                if (cur == this.head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur == this.last) {
                        this.last = this.last.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if (cur.val == key) {
                //开始删除
                if (cur == this.head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur == this.last) {
                        this.last = this.last.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = this.head;
        int log = 0;
        while(cur != null) {
            log++;
            cur = cur.next;
        }
        return log;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("");
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = this.last = null;
    }
}
